package com.wenlin.myproduction.ui;

import android.content.Context;
import android.widget.EditText;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.lxj.xpopup.core.BottomPopupView;
import com.wenlin.myproduction.R;
import com.wenlin.myproduction.bean.PlayerBean;
import com.wenlin.myproduction.util.Checker;
import com.wenlin.myproduction.util.ToastUtils;
import com.wenlin.myproduction.util.UI;

public class EditPlayerDialog extends BottomPopupView {
    private EditText etName, etStar;
    private TextView tv;
    private PlayerBean bean;
    private OnCallback onCallback;

    public EditPlayerDialog(@NonNull Context context) {
        super(context);
    }

    public EditPlayerDialog(@NonNull Context context, PlayerBean bean, OnCallback onCallback) {
        super(context);
        this.bean = bean;
        this.onCallback = onCallback;
    }

    @Override
    protected int getImplLayoutId() {
        return R.layout.dialog_edit_player;
    }

    @Override
    protected void onCreate() {
        super.onCreate();
        etName = findViewById(R.id.et1);
        etStar = findViewById(R.id.et2);
        tv = findViewById(R.id.tv);
        if (bean != null) {
            UI.setText(etName, bean.getName());
            UI.setText(etStar, bean.getStar());
        }
        UI.setOnClickListener(tv, v -> {
            String name = UI.getEtText(etName);
            if (Checker.hasNoWord(name)) {
                ToastUtils.getInstance().showInfo("请输入名字");
                return;
            }
            String star = UI.getEtText(etStar);
            if (Checker.hasNoWord(star)) {
                ToastUtils.getInstance().showInfo("请输入星级");
                return;
            }
            if (bean == null) bean = new PlayerBean();
            bean.setName(name);
            bean.setStar(Integer.parseInt(star));
            if (onCallback != null) onCallback.onCallback(bean);
            dismiss();
        });
    }

    public interface OnCallback {
        void onCallback(PlayerBean bean);
    }
}
